using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CutBlock : MonoBehaviour
{
    public PagePropertie currentPageProperties;
    private GameObject avatar;
    private Square cutZone;
    private Square bottomSquareToCut;
    private Square topSquareToCut;
    
    public void Build()
    {
        avatar = new GameObject();
        cutZone = avatar.AddComponent<Square>();

        cutZone.transform.parent = this.transform;

        cutZone.Build(new Vector3(0 - 0.08f, 0, 0),
                    new Vector3(0 - 0.08f, 1.0f, 0),
                    new Vector3(1.0f, 1.0f, 0),
                    new Vector3(1.0f, 0, 0),

                    new Vector2(0, 0),
                    new Vector2(0, 1),
                    new Vector2(1, 1),
                    new Vector2(1, 0),

                    "CutBlock", "CutBlock0");

        cutZone.renderer.material = (Material)Resources.Load("CutBlockMat");
    }

    public int cut(Sheet sheet)
    {
        List<Square> bottomSquaresList = sheet.getBottomSquaresList();
        List<Square> topSquaresList = sheet.getTopSquaresList();
        int bottomSquaresNb = bottomSquaresList.Count;

        int count = 0;

        for (int i = 0; i < bottomSquaresNb; ++i)
        {
            Square pageSquare = bottomSquaresList[i];

            if (pageSquare.intersect(cutZone))
            {
                pageSquare.destroy();
                topSquaresList[i].destroy();
                //topSquaresList[i].renderer.material.SetColor("_Color", Color.red);
                //pageSquare.renderer.material.SetColor("_Color", Color.red);
                bottomSquareToCut = pageSquare;
                topSquareToCut = topSquaresList[i];
                count++;
            }
        }

        return count;
    }

    public void uncut()
    {
        bottomSquareToCut.rebuild();
        topSquareToCut.rebuild();
    }

    public bool intersect(HeroManager heroManager)
    {
        /*GameObject sphereGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphereGO.transform.position = heroManager.transform.position;*/

        return false;
    }

    public Square getCutZone()
    {
        return cutZone;
    }

	// Use this for initialization
	void Start ()
    {
        float x = currentPageProperties.right_limit - transform.position.x - 2.0f;
        float y = currentPageProperties.floor_height - transform.position.y;

        transform.Translate(new Vector3(x, y, 0f));
	}

	// Update is called once per frame
	void Update ()
    {
	}
}
